package threading;


import java.util.concurrent.atomic.AtomicInteger;

//CAS实现原子类   不用锁也能实现原子操作
public class ThreadDemo26AtomicInteger {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger num = new AtomicInteger(0); //初始化为0

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 500; i++) {
                //后置++
                num.getAndIncrement();
            }
        });


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 500; i++) {
                //后置++
                num.getAndIncrement();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(num.get());
    }

}
